package demo5;

import java.util.LinkedList;
import java.util.List;

public class MyDoubleLinkedList {

    static class ListNode {
        public ListNode prev;
        public ListNode next;
        public int val;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head = null;
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
            node.prev = cur;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index < 0) {
            return false;
        }
        if(index == 0) {
            addFirst(data);
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            if(cur == null) {
                return false;
            }
            index--;
        }
        node.prev = cur.prev;
        cur.prev.next = node;
        node.next = cur;
        cur.prev = node;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            throw new RuntimeException("链表为空！");
        }
        ListNode cur = head;
        if(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
            return;
        }
        while(cur != null) {
            if(cur.val == key) {
                if(cur.next != null) {
                    cur.next.prev = cur.prev;
                }
                cur.prev.next = cur.next;
                return;
            }
            cur = cur.next;
        }
        if(cur == null) {
            throw new RuntimeException("没有你要删除的节点！");
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            throw new RuntimeException("链表为空！");
        }
        ListNode cur = head;
        while(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
        }
        int flag = 0;
        while(cur != null) {
            if(cur.val == key) {
                if(cur.next != null) {
                    cur.next.prev = cur.prev;
                }
                cur.prev.next = cur.next;
                flag = 1;
            }
            cur = cur.next;
        }
        if(flag == 0) {
            throw new RuntimeException("没有你要删除的所有key！");
        }

    }
    //得到单链表的长度
    public int size() {
        if(head == null) {
            return 0;
        }
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display() {
        if(head == null) {
            throw new RuntimeException("链表为空！");
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.next != null) {
                System.out.print(cur.val + "->");
            }else {
                System.out.print(cur.val);
            }
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear() {
        if(head == null) {
            throw new RuntimeException("链表为空！");
        }
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
//            cur = null;
            cur = curNext;
        }
        head = null;
    }

    public static void main(String[] args) {
        MyDoubleLinkedList myDoubleLinkedList = new MyDoubleLinkedList();
        myDoubleLinkedList.addFirst(2);
        myDoubleLinkedList.addFirst(2);
        myDoubleLinkedList.addFirst(3);
        myDoubleLinkedList.addFirst(5);
        myDoubleLinkedList.display();

        myDoubleLinkedList.addLast(6);
        myDoubleLinkedList.display();

        myDoubleLinkedList.addIndex(2,9);
        myDoubleLinkedList.display();

        System.out.println(myDoubleLinkedList.size());
        myDoubleLinkedList.removeAllKey(2);
        System.out.println(myDoubleLinkedList.size());
        myDoubleLinkedList.display();

        myDoubleLinkedList.clear();
        myDoubleLinkedList.display();
    }
}
